package com.example.hsh.homefragment.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.view.menu.MenuBuilder;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.transition.TransitionManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.Toast;

import com.example.hsh.homefragment.MainActivity;
import com.example.hsh.homefragment.R;
import com.example.hsh.homefragment.rx.presenter.RxBasePresenter;
import com.example.hsh.homefragment.rx.view.RxBaseView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by HSH on 2016/12/13.
 */

public abstract class BaseFragment<Presenter extends RxBasePresenter> extends Fragment{
    protected final String FRAGMENT_STATE_HIDEN = "fragment_state_hiden";
    private static final String TAG = "BaseFragment";

    protected static final String SHWO_TOOL_BAR = "shwo_tool_bar";


    protected Context mContext;
    private Unbinder unbinder;

    private String title;

    protected boolean showToolbar;

    private Presenter rxBasePresenter;
    @Nullable
    @BindView(R.id.toolBar)
    Toolbar toolbar;

    @Override
    public void onAttach(Context context) {
        mContext = context;
        super.onAttach(context);
    }

    @SuppressWarnings(value = "unchecked")
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (userPresenter()) {
            rxBasePresenter = createPresenter();
            rxBasePresenter.attach(getBaseView());
        }

        //兼容低版本support v4 低版本下Fragment的重叠问题
        if (savedInstanceState != null) {
            boolean isHidden = savedInstanceState.getBoolean(FRAGMENT_STATE_HIDEN);

            if (isHidden) {
                getFragmentManager().beginTransaction().hide(this).commit();
            }else{
                getFragmentManager().beginTransaction().show(this).commit();
            }
        }

        initData(savedInstanceState);
    }

    protected boolean userPresenter() {
        return true;
    }

    protected abstract Presenter createPresenter();

    @SuppressWarnings(value = "unchecked")
    protected <Ui extends RxBaseView> Ui getBaseView() {
        return (Ui) this;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (getLayoutResource() > 0) {
            View view =  inflater.inflate(getLayoutResource(), container, false);
            unbinder = ButterKnife.bind(this, view);
            iniView(view, savedInstanceState);

            Bundle bundle = getArguments();
            showToolbar = bundle.getBoolean(SHWO_TOOL_BAR, false);

            if (toolbar != null) {

                if (!showToolbar) {
                    toolbar.setVisibility(View.GONE);
                    return view;
                }
                toolbar.setVisibility(View.VISIBLE);

                toolbar.setTitle(TextUtils.isEmpty(title) ? ((MainActivity) mContext).getTitle() : title);
                toolbar.setNavigationOnClickListener(this::onNavigationClick);
                toolbar.setPopupTheme(R.style.BaseMenuTheme);
                if (inflateMenu() > 0) {
                    toolbar.inflateMenu(inflateMenu());
                    toolbar.setOnMenuItemClickListener(this::onMenuItemClick);
                }
                //解决Icon不显示问题，适合高版本的support包
                MenuBuilder menu = (MenuBuilder)toolbar.getMenu();
                menu.setOptionalIconsVisible(true);
            }

            return view;
        }
        return null;
    }

    protected int inflateMenu(){
        return 0;
    }

    protected boolean onMenuItemClick(MenuItem menuItem) {
        return false;
    }

    protected void onNavigationClick(View view) {
        ((MainActivity) mContext).onBackPressed();
    }

    /**
     * 进入Fragment页面的动画
     * @return
     */
    protected  Animation exitAnimation(){
        return null;
    }

    /**
     * 退出Fragment页面的动画
     * @return
     */
    protected Animation enterAimation(){
        return null;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        requestData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
        if (rxBasePresenter != null) {
            rxBasePresenter.detach();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(FRAGMENT_STATE_HIDEN, isHidden());
        outState.putBoolean(SHWO_TOOL_BAR, showToolbar);
    }

    protected void setTitle(String title){
        this.title = title;
    }

    protected String getTitle(){
        if (TextUtils.isEmpty(title)) {
            return (String) ((MainActivity) mContext).getTitle();
        }
        return title;
    }

    protected void requestData(){}

    protected abstract void iniView(View rootView, @Nullable Bundle savedInstanceState);
    protected abstract void initData(Bundle savedInstanceState);
    protected abstract int getLayoutResource();
}
